package com.item;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * TreeSet集合
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        //集合只能存储引用数据类型
//        test0();
//        test1();
//        test2();
    }

    private static void test0() {
        TreeSet<Integer> set1 = new TreeSet<>();
        //1--->包装成Integer对象--->自动装箱
        set1.add(1);
        set1.add(3);
        set1.add(2);
        set1.add(5);
        set1.add(4);
        for (Integer integer : set1){
            System.out.println(integer);
        }
    }

    //Student类调用compareto方法
//    public static void test1() {
//        TreeSet<Student> set = new TreeSet<>();
//        set.add(new Student("张三", 19));
//        set.add(new Student("李四", 18));
//        set.add(new Student("王五", 21));
//        set.add(new Student("赵六", 20));
//        for (Student student : set){
//            System.out.println(student.getName()+"---"+student.getAge());
//        }
//    }
    public static void test2() {
        //匿名内部类
//        Comparator<Student> comparator = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                //正序排：o1-o2
//                //倒序排：o2-o1
//                return o1.getAge()-o2.getAge();
//            }
//        };
        //Lambda表达
        Comparator<Student> comparator = (o1,o2)->o1.getAge()-o2.getAge();
        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("张三", 19));
        set.add(new Student("李四", 18));
        set.add(new Student("王五", 21));
        set.add(new Student("赵六", 20));
        for (Student student : set){
            System.out.println(student.getName()+"---"+student.getAge());
        }
    }
    //可变参数必须是形参的最后一个
    public static int sum(int...a){
        int total = 0;
        for(int i =  0; i < a.length; i++){
            total += a[i];
        }
        return total;
    }
}
